﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TyNetCoreBlog.Models.Dto.Blog;
using TyNetCoreBlog.Models.Dto.Category;
using TyNetCoreBlog.Models.Dto.FriendlyLink;
using TyNetCoreBlog.Models.Dto.Tag;
using TyNetCoreBlog.Models.Dto.User;
using TyNetCoreBlog.Models.Entities;

namespace TyNetCoreBlog.Helpers
{
    public class TyMappingProfile : Profile
    {
        public TyMappingProfile()
        {
            
            // 输出
            CreateMap<User, UserDto>();
            CreateMap<Category, CategoryDto>()
                .ForMember(dest => dest.BlogCount,
                    conf => conf.MapFrom(src => src.Blogs.Count));

            CreateMap<Blog, BlogDto>()
                .ForMember(dest => dest.AuthorNickname,
                    conf => conf.MapFrom(src => src.User.Nickname))
                .ForMember(dest => dest.CategoryName,
                    conf => conf.MapFrom(src => src.Category.Name));


            CreateMap<Tag, TagDto>()
                .ForMember(dest => dest.BlogCount,
                    conf => conf.MapFrom(src => src.BlogTags.Count));

            CreateMap<FriendlyLink, FriendlyLinkDto>();

            // 创建
            CreateMap<CategoryForCreationDto, Category>();
            CreateMap<BlogForCreationDto, Blog>();
            CreateMap<TagForCreationDto, Tag>();
            CreateMap<FriendlyLinkForCreationDto, FriendlyLink>();
            // 更新
            CreateMap<UserForUpdateDto, User>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember, dMember) =>
                                srcMember != null));

            CreateMap<CategoryFroUpdateDto, Category>();
            CreateMap<BlogForUpdateDto, Blog>();
            CreateMap<TagForUpdateDto, Tag>();
            CreateMap<FriendlyLinkForUpdateDto, FriendlyLink>();
        }

    }
}
